package com.ruoyi.creditlife.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.creditlife.domain.Doctor;
import com.ruoyi.creditlife.domain.DoctorAppointment;
import com.ruoyi.creditlife.domain.DoctorSchedule;
import com.ruoyi.creditlife.domain.Hospital;
import com.ruoyi.creditlife.mapper.DoctorAppointmentMapper;
import com.ruoyi.creditlife.mapper.DoctorMapper;
import com.ruoyi.creditlife.mapper.DoctorScheduleMapper;
import com.ruoyi.creditlife.mapper.HospitalMapper;
import com.ruoyi.creditlife.service.IHospitalService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.utils.ip.AddressUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 医院数据Service业务层处理
 * 
 * @author ddd
 * @date 2025-07-11
 */
@Service
public class HospitalServiceImpl implements IHospitalService 
{
    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private DoctorScheduleMapper doctorScheduleMapper;
    @Autowired
    private DoctorAppointmentMapper doctorAppointmentMapper;

    private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);

    @Override
    public List<Doctor> getHospitalDoctor(Long hospitalId) {
        Doctor doctor = new Doctor();
        doctor.setHospitalId(hospitalId);
      return doctorMapper.selectDoctorList(doctor);
    }

    @Override
    public List<DoctorSchedule> getDoctorSchedule(Long doctorId) {
        DoctorSchedule doctorSchedule = new DoctorSchedule();
        doctorSchedule.setDoctorId(doctorId);
       return doctorScheduleMapper.selectDoctorScheduleList(doctorSchedule);
    }

    @Override
    public int insertApointment(DoctorSchedule doctorSchedule) {
        DoctorAppointment doctorAppointment = new DoctorAppointment();
        doctorAppointment.setDoctorName(doctorSchedule.getDoctorName());
        doctorAppointment.setUserId(SecurityUtils.getUserId());
        doctorAppointment.setScheduleId(doctorSchedule.getScheduleId());
        doctorAppointment.setStatus("完成");
        doctorAppointment.setCreateTime(new Date());
        String availableStr = doctorSchedule.getAvailable();
        int availableInt = Integer.parseInt(availableStr);
        if (availableInt <= 0) {
            log.info("该医生当前没有可用号数");
            return -1;
        }
        int result = availableInt - 1;
        String resultStr = result+"";
        doctorSchedule.setAvailable(resultStr);
        doctorScheduleMapper.updateDoctorSchedule(doctorSchedule);
       return doctorAppointmentMapper.insertDoctorAppointment(doctorAppointment);
    }

    @Override
    public List<DoctorAppointment> getAppointment() {
        DoctorAppointment doctorAppointment = new DoctorAppointment();
        doctorAppointment.setUserId(SecurityUtils.getUserId());
      return   doctorAppointmentMapper.selectDoctorAppointmentList(doctorAppointment);
    }

    @Override
    public int CancelAppointment(DoctorAppointment doctorAppointment) {
      DoctorSchedule doctorSchedule = doctorScheduleMapper.selectDoctorScheduleByScheduleId(doctorAppointment.getScheduleId());
        String availableStr = doctorSchedule.getAvailable();
        int availableInt = Integer.parseInt(availableStr);
        int result = availableInt + 1;
        String resultStr = result+"";
        doctorSchedule.setAvailable(resultStr);
        doctorScheduleMapper.updateDoctorSchedule(doctorSchedule);
        doctorAppointmentMapper.deleteDoctorAppointmentByAppointmentId(doctorAppointment.getAppointmentId());
        return 1;
    }

    /**
     * 查询医院数据
     * 
     * @param hospitalId 医院数据主键
     * @return 医院数据
     */
    @Override
    public Hospital selectHospitalByHospitalId(Long hospitalId)
    {
        return hospitalMapper.selectHospitalByHospitalId(hospitalId);
    }

    /**
     * 查询医院数据列表
     * 
     * @param hospital 医院数据
     * @return 医院数据
     */
    @Override
    public List<Hospital> selectHospitalList(Hospital hospital)
    {
        return hospitalMapper.selectHospitalList(hospital);
    }

    /**
     * 新增医院数据
     * 
     * @param hospital 医院数据
     * @return 结果
     */
    @Override
    public int insertHospital(Hospital hospital)
    {
        return hospitalMapper.insertHospital(hospital);
    }

    /**
     * 修改医院数据
     * 
     * @param hospital 医院数据
     * @return 结果
     */
    @Override
    public int updateHospital(Hospital hospital)
    {
        return hospitalMapper.updateHospital(hospital);
    }

    /**
     * 批量删除医院数据
     * 
     * @param hospitalIds 需要删除的医院数据主键
     * @return 结果
     */
    @Override
    public int deleteHospitalByHospitalIds(Long[] hospitalIds)
    {
        return hospitalMapper.deleteHospitalByHospitalIds(hospitalIds);
    }

    /**
     * 删除医院数据信息
     * 
     * @param hospitalId 医院数据主键
     * @return 结果
     */
    @Override
    public int deleteHospitalByHospitalId(Long hospitalId)
    {
        return hospitalMapper.deleteHospitalByHospitalId(hospitalId);
    }


}
